<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 分配员管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_peisong_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'分配员列表'),
			array('op'=>'edit','title'=>'添加分配员'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：非抢单模式下，就需要分配员来分配订单给配送员！
		','list'=>array(
			array('name'=>'title','title'=>'分配员姓名','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'name','title'=>'账号','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择分配员！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_peisong_worker',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_peisong_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$item['upass']='';
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回分配员列表'),
		),
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'分配员姓名','name'=>'title','help'=>''),
					array('type'=>'string','title'=>'联系电话','name'=>'mobile','help'=>''),
					array('type'=>'choose','title'=>'关联会员','name'=>'mid','help'=>'','plugin'=>'core','action'=>'member.get_member','choose_title'=>'选择会员'),
					array('name'=>'name','title'=>'账号','type'=>'string'),
					array('name'=>'upass','title'=>'密码','type'=>'string'),
					array('type'=>'switch','title'=>'状态','name'=>'status','help'=>''),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'mobile'=>trim($postdata['mobile']),
		'name'=>trim($postdata['name']),
		'upass'=>base64_encode($postdata['upass']),
		'mid'=>intval($postdata['mid']),
		'status'=>intval($postdata['status']),
	);
	$uname=pdo_get('jy_peisong_worker',array('name'=>$data['name'],'uniacid'=>$_SESSION['uniacid']));
	$id=intval($postdata['id']);
	if($id){
		if($uname && $uname['id']!=$id){
			exi('登陆账户名不可以重复','error');	
		}
		if(!$postdata['upass']){
			unset($postdata['upass']);
		}
		pdo_update('jy_peisong_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		if($uname){
			exi('登陆账户名不可以重复','error');	
		}
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_peisong_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_peisong_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}